package com.wise.schedule.sample2quartz.config;

import com.wise.schedule.sample2quartz.task.ScheduleTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;

@Configuration
@Profile("sample2")
public class ScheduleConfig {

    @Autowired
    private ScheduleTask scheduleTask;

    @Bean
    public MethodInvokingJobDetailFactoryBean scheduleTaskDetail() {
        System.out.println("-- scheduleTaskDetail init ---");
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
        bean.setTargetObject(scheduleTask);
        bean.setTargetMethod("doTask");
        bean.setConcurrent(false);
        return bean;
    }

    @Bean
    public CronTriggerFactoryBean scheduleTrigger() {
        CronTriggerFactoryBean scheduleTrigger = new CronTriggerFactoryBean();
        scheduleTrigger.setJobDetail(scheduleTaskDetail().getObject());
        scheduleTrigger.setCronExpression("0/5 * * * * ?");
        return scheduleTrigger;
    }
}